import { verifyToken } from '@/server/lib/jwt';

export default defineEventHandler((event) => {
  const currentUrl = event.node.req.url;
  if (currentUrl?.startsWith('/api')) {
    const notAuthUrls = [
      '/api/user/login',
    ];
    
    if (!notAuthUrls.includes(currentUrl as string)) {
      const headers = getHeaders(event);
      const authorization = headers['authorization'] as string;
  
      if (authorization) {
        const token = authorization.split(' ')[1];
        if (!verifyToken(token)) {
          return {
            code: 403,
            msg: '登录失效'
          }
        }
      } else {
        return {
          code: 403,
          msg: '登录失效'
        }
      }
    }
  }
})